package com.dome.gateway.route;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description:
 * @author: hz
 * @time: 2021/4/28 20:07
 */
@Slf4j
public class DynamicRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {
    private ZuulProperties zuulProperties;

    private GatewayApiRouteProperties gatewayApiRouteProperties;

    public DynamicRouteLocator(String servletPath, ZuulProperties properties,GatewayApiRouteProperties gatewayApiRouteProperties) {
        super(servletPath, properties);
        this.zuulProperties = properties;
        this.gatewayApiRouteProperties = gatewayApiRouteProperties;
    }

    /**
     * 自动刷新功能
     */
    @Override
    public void refresh() {
        doRefresh();
    }

    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<>();
        // 加载application.yml中的路由表
        routesMap.putAll(super.locateRoutes());
        // 加载db中的路由表
        routesMap.putAll(locateRoutesFromProperties());

        // 统一处理一下路由path的格式
        LinkedHashMap<String, ZuulProperties.ZuulRoute> values = new LinkedHashMap<>();
        for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : routesMap.entrySet()) {
            String path = entry.getKey();
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            if (StringUtils.hasText(this.zuulProperties.getPrefix())) {
                path = this.zuulProperties.getPrefix() + path;
                if (!path.startsWith("/")) {
                    path = "/" + path;
                }
            }
            values.put(path, entry.getValue());
        }

        return values;
    }

    private Map<String, ZuulProperties.ZuulRoute> locateRoutesFromProperties() {
        Map<String, ZuulProperties.ZuulRoute> routes = new LinkedHashMap<>();

        Map<String,GatewayApiRoute> results = gatewayApiRouteProperties.getGatewayApiRoutes();
        for (String key : results.keySet()) {
            GatewayApiRoute result = results.get(key);
            if (StringUtils.isEmpty(result.getPath()) ) {
                continue;
            }
            if (StringUtils.isEmpty(result.getServiceId()) && StringUtils.isEmpty(result.getUrl())) {
                continue;
            }
            ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();
            try {
                BeanUtils.copyProperties(result, zuulRoute);
            } catch (Exception e) {
                log.error("加载配置文件复制失败",e);
            }
            routes.put(zuulRoute.getPath(), zuulRoute);
        }

        return routes;
    }

}